package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/activityItem")
public class ActivityItemController {

    @Autowired
    private IActivityItemService activityItemService;
    @Autowired
    private IActivityService activityService;


    //@RequiresPermissions(value = {"activityItem:list","活动详情页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<ActivityItem> pageInfo = activityItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activityItem/list";
    }

    @RequestMapping("/delete")
    //@RequiresPermissions(value = {"activityItem:delete","活动详情删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            activityItemService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    //@RequiresPermissions(value = {"activityItem:saveOrUpdate","活动详情新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(ActivityItem activityItem){
        if (activityItem.getId() != null) {
            activityItemService.update(activityItem);
        }else {
            activityItemService.save(activityItem);
        }
        return new JsonResult();
    }

    //跳转到数据统计页面,并且插入数据到choice表中
    @RequestMapping("/insertToChoice")
    @ResponseBody
    public JsonResult insertToChoice(Long optionsRadios,Long id){//当前用户id和选择的题号id
        Date date = new Date();

        //是否评论过
        activityItemService.insertToChoice(UserContext.getCurrentUser().getId(),optionsRadios,date,id);

        //选中的那个选项数量加一
        activityItemService.updateRelation(optionsRadios);



        return new JsonResult();
    }


    @RequestMapping("/count")
    public String count(Model model,Long id){
        //查出所有的人数，并且统计:选择它的人数/总人数
        //每一次查之前将提交的都加一
        System.out.println("-----------");
        //统计所有的人数，activity_id为某一个的
        Integer totalPerson = activityItemService.findSum(id);
        model.addAttribute("totalPerson",totalPerson);

        //查询当前要投票的活动
        Activity activity = activityService.selectById(id);
        model.addAttribute("activity",activity);


        List<ActivityItem> activityItems =  activityItemService.listAll(id);
        model.addAttribute("activityItems",activityItems);
        return "/activity/result";
    }


    //修改状态
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id,Boolean status){
        activityItemService.updateStatusById(id,status);
        return  new JsonResult();
    }

}
